<?php

/*

COMP 353F
Dr. B Desai
Final Project
Group #11

6330746 Nicholas CONSTANTINIDIS
9532862 Jacqueline FU
5484537 Claudio Javier LOPEZ FLORES
9218416 Que Tung NGUYEN

*/

  session_start();
?>

<!DOCTYPE html>
<html lang="en">
<head>
        <title>CoBAGSys - Home</title>
        <link type="text/css" rel="stylesheet" href="css/mystyle.css">
</head>
<body>
    <?php
      require 'menus.php';
    ?>
	<?php
		// Return list of members
		function getMembers() 
		{
			$db = new Connect();

			$sql = "SELECT mID, mName, email, suspended FROM MemberInfo";
			
			$result = $db->query($sql);
			
			if(!$result || mysql_num_rows($result) == 0)
			{
				return;
			}
			else
			{
				$rows = $db->to_array($result);
				return $rows;
			}
	    	}
		
		// Perform the member status change
		function changeMember() 
		{
			$db = new Connect();
			
  			$changed=$_POST['status'];
			$sql = "UPDATE MemberInfo SET suspended= '" . $changed . "' " .
				"WHERE mID = " . $_GET['mid'];
			
			$result = $db->query($sql);
			
			if(!$result || mysql_affected_rows() == 0) // Failed member status change
			{
				return false;
			}
			else // Member status changed
			{
				return true;
			}
		}

		// Perform the member delete
		function removeMember() 
		{
			$db = new Connect();
			
			$sql = "DELETE FROM MemberInfo, Guest USING MemberInfo INNER JOIN Guest" .
				"ON MemberInfo.email = Guest.email WHERE m.mID = " . $_GET['mid'];
			
			$result = $db->query($sql);
			
			if(!$result || mysql_affected_rows() == 0) // Failed member delete
			{
				return false;
			}
			else // Member deleted
			{
				return true;
			}
		}

		$changemember_failed = false;
		if (isset($_POST["change"]))
			$changemember_failed = !changeMember();
		$removemember_failed = false;
		if (isset($_POST["remove"]))
			$removemember_failed = !removeMember();
    	?>
    <div id="main">
    	<p class="title">Member Status</p>
	<?php
		if ($changemember_failed == true)
			print "<p class=\"error\">FAILED CHANGING MEMBER STATUS!</p>";
		if ($removemember_failed == true)
			print "<p class=\"error\">FAILED REMOVING MEMBER!</p>";

		$rows = getMembers();
		if (!isset($rows))
		{
			echo "No member found.";
		}
		else
		{
			echo "<p>This is the list of members in the group</p>";
			echo "<table border=1>";
			echo "<tr>";
			echo "<th>ID</th>";
			echo "<th>Name</th>";
			echo "<th>Status</th>";
			echo "<th>Choose Status</th>";
			echo "<th>Confirm Status</th>";
			echo "<th>Remove Member</th>";
			echo "</tr>";

			$rowcount = count($rows);
			for ($i = 0; $i < $rowcount; $i++)
			{
				echo "<tr>";
				echo '<td>'. $rows[$i]['mID'] .'</td>';
				echo '<td>'. $rows[$i]['mName'] .'</td>';
				if($rows[$i]['suspended'] == 1)
					echo "<td>suspended</td>";
				else
					echo "<td>not suspended</td>";
				echo '<td><form name="changeMem" method="post" action="adminmembers.php?mid=%20'. $rows[$i]['mID'] .' ">
					<input type="radio" name="status" value="0">Not Suspended<br>
					<input type="radio" name="status" value="1">Suspended
					
					</td>';
				echo '<td><input type="submit" class="smallButton" name="change" value="Change"/></form></td>';
				echo '<td><form method="post" action="adminmembers.php?mid=%20'. $rows[$i]['mID'] .' ">
					<input type="submit" class="smallButton" name="remove" value="Remove"/></form></td>';
				echo "</tr>";
			}
			echo "</table>";
	
			echo "<p></p>";
			//echo '<form onclick="changeMember()" ><input type="submit" name="submit" value="Change Member Status"/></form>';
		}
	?>
    </div>
</body>
</html>
